Imports Clo = Enhancements.Core.Logging
Imports Gen = System.Collections.Generic
Imports Glo = System.Globalization
Imports Iou = System.IO
Imports Ref = System.Reflection
Imports Reg = System.Text.RegularExpressions
Imports Sys = System
Imports Tex = System.Text
Imports Xsd = System.Xml.Schema
Imports W3c = System.Xml

Namespace Enhancements.Basic.Primitives
    ''' <summary>Represents a format applicable to a free text part.</summary>
    <Serializable(), DebuggerStepThrough()> _
    Public NotInheritable Class DateTimeFormatText
        Inherits DateTimeFormat

        ''' <summary>Initializes a DateTimeFormatText instance with default values.</summary>
        Public Sub New()
        End Sub

        ''' <summary>Initializes a DateTimeFormatText instance with defined values.</summary>
        ''' <param name="_value">Value to use.</param>
        ''' <exception cref="IsNullException">_value is null.</exception>
        Public Sub New(ByVal _value As System.String)
            Value = _value
        End Sub

        Private __Value As System.String = Sys.String.Empty
        ''' <summary>Gets and sets the value to use. The default value is 'System.String.Empty'.</summary>
        ''' <exception cref="IsNullException">_value is null.</exception>
        Public Property Value() As System.String
            Get
                Return __Value
            End Get
            Set(ByVal _value As System.String)
                _value.IsNullNot("Value._value", True)

                __Value = _value
            End Set
        End Property

        ''' <summary>Builds a format expression according to the instance values.</summary>
        ''' <returns> </returns>
        Public Overrides Function ExpressionBuild() As System.String
            Dim _returnValueStringBuilder As Tex.StringBuilder = New Tex.StringBuilder(Sys.String.Empty)
            _returnValueStringBuilder.Append(___ExpressionPrefix)
            _returnValueStringBuilder.Append("text")
            _returnValueStringBuilder.Append(___ExpressionSeparatorTitleProperties)
            _returnValueStringBuilder.Append("value")
            _returnValueStringBuilder.Append(___ExpressionSeparatorPropertyNameValue)
            _returnValueStringBuilder.Append(Value.ToString())
            _returnValueStringBuilder.Append(___ExpressionSeparatorProperties)
            _returnValueStringBuilder.Append(___ExpressionSuffix)

            Return _returnValueStringBuilder.ToString()
        End Function

        ''' <summary>Formats a supplied value according to the instance values.</summary>
        ''' <param name="_value">Value to format.</param>
        ''' <returns> </returns>
        Public Overrides Function Format(ByVal _value As Sys.DateTime) As System.String
            Dim _returnValue As System.String = Value

            Return _returnValue
        End Function

        Friend Const ___RegularExpression As System.String = "^{text:value=(.*);}$"

        ''' <summary>Tries to parse a format expression, returning an according instance in the _result parameter and True in the return value, when valid. When not, null is put in _result and False is returned.</summary>
        ''' <param name="_expression">Expression to inspect.</param>
        ''' <param name="_result">Instance returned when a valid format expression is used.</param>
        ''' <returns>Whether the operation proceeded normally.</returns>
        Public Shared Function TryParse(ByVal _expression As System.String, ByRef _result As DateTimeFormatText) As System.Boolean
            Dim _returnValue As System.Boolean = False
            Try
                _expression.IsInRegularExpression("_expression", ___RegularExpression, True)
                Dim _value As System.String = Reg.Regex.Matches(_expression, ___RegularExpression).Item(0).Groups(1).Value
                _result = New DateTimeFormatText(_value)
                _returnValue = True
            Catch
                _result = Nothing
            End Try

            Return _returnValue
        End Function

    End Class
End Namespace
